// $ANTLR 2.7.6 (2005-12-22): "numberExpression.g" -> "NumberExpression.java"$

package com.aote.expression.antlr;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.aote.expression.ParamProcessorFactory;
import com.aote.expression.unit.UnitProcessorFactory;
import java.util.Map;
import com.browsesoft.user.User;
import org.w3c.dom.Element;

import antlr.TokenBuffer;
import antlr.TokenStreamException;
import antlr.TokenStreamIOException;
import antlr.ANTLRException;
import antlr.LLkParser;
import antlr.Token;
import antlr.TokenStream;
import antlr.RecognitionException;
import antlr.NoViableAltException;
import antlr.MismatchedTokenException;
import antlr.SemanticException;
import antlr.ParserSharedInputState;
import antlr.collections.impl.BitSet;

public class NumberExpression extends antlr.LLkParser       implements NumberExpressionTokenTypes
 {

    private double process(String param,Map attrs,User loginUser,HTMLBasicComponent component,Element config)
    {
        String str = ParamProcessorFactory.getInstance().process(param,attrs,loginUser,component,config);
        if(str == null || str.trim().equals("")) {
          str = "0";
        }
        return Double.parseDouble(str);
    }

	private String getParamString(String param) {
		int len = param.length();
		return param.substring(1,len - 1);
	}

protected NumberExpression(TokenBuffer tokenBuf, int k) {
  super(tokenBuf,k);
  tokenNames = _tokenNames;
}

public NumberExpression(TokenBuffer tokenBuf) {
  this(tokenBuf,1);
}

protected NumberExpression(TokenStream lexer, int k) {
  super(lexer,k);
  tokenNames = _tokenNames;
}

public NumberExpression(TokenStream lexer) {
  this(lexer,1);
}

public NumberExpression(ParserSharedInputState state) {
  super(state,1);
  tokenNames = _tokenNames;
}

	public final double  expression(
		Map attrs,User loginUser,HTMLBasicComponent component,Element config
	) throws RecognitionException, TokenStreamException {
		double result=0;
		
		
			double b;
		
		
		try {      // for error handling
			result=mulDiv(attrs,loginUser,component,config);
			{
			_loop5:
			do {
				switch ( LA(1)) {
				case ADD:
				{
					{
					match(ADD);
					b=mulDiv(attrs,loginUser,component,config);
					if ( inputState.guessing==0 ) {
						result += b;
					}
					}
					break;
				}
				case SUB:
				{
					{
					match(SUB);
					b=mulDiv(attrs,loginUser,component,config);
					if ( inputState.guessing==0 ) {
						result -= b;
					}
					}
					break;
				}
				default:
				{
					break _loop5;
				}
				}
			} while (true);
			}
		}
		catch (RecognitionException ex) {
			if (inputState.guessing==0) {
				reportError(ex);
				recover(ex,_tokenSet_0);
			} else {
			  throw ex;
			}
		}
		return result;
	}
	
	public final double  mulDiv(
		Map attrs,User loginUser,HTMLBasicComponent component,Element config
	) throws RecognitionException, TokenStreamException {
		double result=0;
		
		
			double b;
		
		
		try {      // for error handling
			result=item(attrs,loginUser,component,config);
			{
			_loop10:
			do {
				switch ( LA(1)) {
				case MULT:
				{
					{
					match(MULT);
					b=item(attrs,loginUser,component,config);
					if ( inputState.guessing==0 ) {
						result *= b;
					}
					}
					break;
				}
				case DIV:
				{
					{
					match(DIV);
					b=item(attrs,loginUser,component,config);
					if ( inputState.guessing==0 ) {
						result = (b == 0 ? 0 : result / b);
					}
					}
					break;
				}
				default:
				{
					break _loop10;
				}
				}
			} while (true);
			}
		}
		catch (RecognitionException ex) {
			if (inputState.guessing==0) {
				reportError(ex);
				recover(ex,_tokenSet_1);
			} else {
			  throw ex;
			}
		}
		return result;
	}
	
	public final double  item(
		Map attrs,User loginUser,HTMLBasicComponent component,Element config
	) throws RecognitionException, TokenStreamException {
		double result=0;
		
		
		
		
		try {      // for error handling
			boolean synPredMatched13 = false;
			if ((((LA(1) >= NUMBER && LA(1) <= LEFT)))) {
				int _m13 = mark();
				synPredMatched13 = true;
				inputState.guessing++;
				try {
					{
					baseItem(attrs,loginUser,component,config);
					match(ID);
					}
				}
				catch (RecognitionException pe) {
					synPredMatched13 = false;
				}
				rewind(_m13);
inputState.guessing--;
			}
			if ( synPredMatched13 ) {
				result=unitItem(attrs,loginUser,component,config);
			}
			else if (((LA(1) >= NUMBER && LA(1) <= LEFT))) {
				result=baseItem(attrs,loginUser,component,config);
			}
			else {
				throw new NoViableAltException(LT(1), getFilename());
			}
			
		}
		catch (RecognitionException ex) {
			if (inputState.guessing==0) {
				reportError(ex);
				recover(ex,_tokenSet_2);
			} else {
			  throw ex;
			}
		}
		return result;
	}
	
	public final double  baseItem(
		Map attrs,User loginUser,HTMLBasicComponent component,Element config
	) throws RecognitionException, TokenStreamException {
		double result=0;
		
		Token  v = null;
		Token  p = null;
		
			double b;
		
		
		try {      // for error handling
			switch ( LA(1)) {
			case NUMBER:
			{
				v = LT(1);
				match(NUMBER);
				if ( inputState.guessing==0 ) {
					result = Double.parseDouble(v.getText());
				}
				break;
			}
			case PARAMS:
			{
				p = LT(1);
				match(PARAMS);
				if ( inputState.guessing==0 ) {
					result = process(getParamString(p.getText()),attrs,loginUser,component,config);
				}
				break;
			}
			case LEFT:
			{
				match(LEFT);
				b=expression(attrs,loginUser,component,config);
				match(RIGHT);
				if ( inputState.guessing==0 ) {
					result=b;
				}
				break;
			}
			default:
			{
				throw new NoViableAltException(LT(1), getFilename());
			}
			}
		}
		catch (RecognitionException ex) {
			if (inputState.guessing==0) {
				reportError(ex);
				recover(ex,_tokenSet_3);
			} else {
			  throw ex;
			}
		}
		return result;
	}
	
	public final double  unitItem(
		Map attrs,User loginUser,HTMLBasicComponent component,Element config
	) throws RecognitionException, TokenStreamException {
		double result=0;
		
		Token  u = null;
		
		try {      // for error handling
			result=baseItem(attrs,loginUser,component,config);
			{
			u = LT(1);
			match(ID);
			if ( inputState.guessing==0 ) {
				
				result=UnitProcessorFactory.getInstance().process(u.getText(), result);
				
			}
			}
		}
		catch (RecognitionException ex) {
			if (inputState.guessing==0) {
				reportError(ex);
				recover(ex,_tokenSet_2);
			} else {
			  throw ex;
			}
		}
		return result;
	}
	
	
	public static final String[] _tokenNames = {
		"<0>",
		"EOF",
		"<2>",
		"NULL_TREE_LOOKAHEAD",
		"ADD",
		"SUB",
		"MULT",
		"DIV",
		"ID",
		"NUMBER",
		"PARAMS",
		"LEFT",
		"RIGHT",
		"WS"
	};
	
	private static final long[] mk_tokenSet_0() {
		long[] data = { 4096L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_0 = new BitSet(mk_tokenSet_0());
	private static final long[] mk_tokenSet_1() {
		long[] data = { 4144L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_1 = new BitSet(mk_tokenSet_1());
	private static final long[] mk_tokenSet_2() {
		long[] data = { 4336L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_2 = new BitSet(mk_tokenSet_2());
	private static final long[] mk_tokenSet_3() {
		long[] data = { 4592L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_3 = new BitSet(mk_tokenSet_3());
	
	}
